// 严格检查
'use strict';

/* 禁止默认事件 */
document.ondragstart = function() {
    return false;
};

/* 格式化日期时间 */
function update_time(date, str) {
    var mat = {};
    mat.M = date.getMonth() + 1; //月份记得加1
    mat.H = date.getHours();
    mat.s = date.getSeconds();
    mat.m = date.getMinutes();
    mat.Y = date.getFullYear();
    mat.D = date.getDate();
    mat.d = date.getDay(); //星期几
    mat.d = check(mat.d);
    mat.H = check(mat.H);
    mat.M = check(mat.M);
    mat.D = check(mat.D);
    mat.s = check(mat.s);
    mat.m = check(mat.m);
    if (str.indexOf(":") > -1) {　　　　　
        mat.Y = mat.Y.toString().substr(2, 2);　　　　
        return mat.Y + "-" + mat.M + "-" + mat.D + " " + mat.H + ":" + mat.m + ":" + mat.s;
    }
    if (str.indexOf("/") > -1) {
        return mat.Y + "-" + mat.M + "-" + mat.D + " " + mat.H + ":" + mat.m + ":" + mat.s;
    }
    if (str.indexOf("-") > -1) {
        return mat.Y + "-" + mat.M + "-" + mat.D + " " + mat.H + ":" + mat.m + ":" + mat.s;
    }
}
/* 检查是不是两位数字，不足补全 */
function check(str) {
    str = str.toString();
    if (str.length < 2) {
        str = '0' + str;
    }
    return str;
}

/* 格式化json排版样式 */
function formatJson(json, options) {
    var reg = null,
        formatted = '',
        pad = 0,
        PADDING = '    ';
    options = options || {};
    options.newlineAfterColonIfBeforeBraceOrBracket = (options.newlineAfterColonIfBeforeBraceOrBracket === true) ? true : false;
    options.spaceAfterColon = (options.spaceAfterColon === false) ? false : true;
    if (typeof json !== 'string') {
        json = JSON.stringify(json);
    } else {
        json = JSON.parse(json);
        json = JSON.stringify(json);
    }
    reg = /([\{\}])/g;
    json = json.replace(reg, '\r\n$1\r\n');
    reg = /([\[\]])/g;
    json = json.replace(reg, '\r\n$1\r\n');
    reg = /(\,)/g;
    json = json.replace(reg, '$1\r\n');
    reg = /(\r\n\r\n)/g;
    json = json.replace(reg, '\r\n');
    reg = /\r\n\,/g;
    json = json.replace(reg, ',');
    if (!options.newlineAfterColonIfBeforeBraceOrBracket) {
        reg = /\:\r\n\{/g;
        json = json.replace(reg, ':{');
        reg = /\:\r\n\[/g;
        json = json.replace(reg, ':[');
    }
    if (options.spaceAfterColon) {
        reg = /\:/g;
        json = json.replace(reg, ':');
    }
    (json.split('\r\n')).forEach(function(node, index) {
        var i = 0,
            indent = 0,
            padding = '';

        if (node.match(/\{$/) || node.match(/\[$/)) {
            indent = 1;
        } else if (node.match(/\}/) || node.match(/\]/)) {
            if (pad !== 0) {
                pad -= 1;
            }
        } else {
            indent = 0;
        }

        for (i = 0; i < pad; i++) {
            padding += PADDING;
        }

        formatted += padding + node + '\r\n';
        pad += indent;
    });
    return formatted;
};

//获取单选框的值
function getRadio(selector) {
    var objs = document.querySelectorAll(selector);
    for(var i=0; i<objs.length; i ++){
        if(objs[i].checked){
            return objs[i].value
        }
    };
}

//Toast提示
function Toast(stata,msg){
    var Toast = document.querySelectorAll('.Toast');
    for(var i=0;i<Toast.length;i++){
        var parentObj = Toast[i].parentNode;
　　     parentObj.removeChild(Toast[i]);
    }
    var icon = '';
    var style = '';

    if(stata == 'res'){
        icon = 'icon-successful';
        style = 'res';
    }else if(stata == 'hint'){
        icon = 'icon-abnormal';
        style = 'hint';
    }else if(stata == 'err'){
        icon = 'icon-failure';
        style = 'err';
    }else if(stata == 'errs'){
        icon = 'icon-abnormal';
        style = 'errs';
    }
    var icons = '';
    if(icon!=''){
        icons = '<i class="iconfont '+icon+'"></i>';
    }
    var str = '<div ondblclick="delToast()" class="Toast '+style+'">'+icons+update_time(new Date(),"HH-mm-ss").slice(11) + ' ' + msg+'</div>';
    document.querySelector('body').insertAdjacentHTML('beforeend', str);
}
// 删除Toast
function delToast(){
    document.querySelector('.Toast').parentNode.removeChild(document.querySelector('.Toast'));
}

//获取元素的纵坐标（相对于窗口）
function getTop(e){
    var offset=e.offsetTop;
    if(e.offsetParent!=null) offset+=getTop(e.offsetParent);
    return offset;
}
//获取元素的横坐标（相对于窗口）
function getLeft(e){
    var offset=e.offsetLeft;
    if(e.offsetParent!=null) offset+=getLeft(e.offsetParent);
    return offset;
}

// 获取屏幕尺寸
function getScreen(){
    var s = "";   
     s += " 网页可见区域宽："+ document.body.clientWidth+"\n";    
     s += " 网页可见区域高："+ document.body.clientHeight+"\n";    
     s += " 网页可见区域宽："+ document.body.offsetWidth + " (包括边线和滚动条的宽)"+"\n";    
     s += " 网页可见区域高："+ document.body.offsetHeight + " (包括边线的宽)"+"\n";    
     s += " 网页正文全文宽："+ document.body.scrollWidth+"\n";    
     s += " 网页正文全文高："+ document.body.scrollHeight+"\n";    
     s += " 网页被卷去的高(ff)："+ document.body.scrollTop+"\n";    
     s += " 网页被卷去的高(ie)："+ document.documentElement.scrollTop+"\n";    
     s += " 网页被卷去的左："+ document.body.scrollLeft+"\n";    
     s += " 网页正文部分上："+ window.screenTop+"\n";    
     s += " 网页正文部分左："+ window.screenLeft+"\n";    
     s += " 屏幕分辨率的高："+ window.screen.height+"\n";    
     s += " 屏幕分辨率的宽："+ window.screen.width+"\n";    
     s += " 屏幕可用工作区高度："+ window.screen.availHeight+"\n";    
     s += " 屏幕可用工作区宽度："+ window.screen.availWidth+"\n";    
     s += " 你的屏幕设置是 "+ window.screen.colorDepth +" 位彩色"+"\n";    
     s += " 你的屏幕设置 "+ window.screen.deviceXDPI +" 像素/英寸"+"\n";    
     alert (s);
}